home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Can't figure this out
- Date: 20 Jan 1996 04:09:43 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Jan19210943@qcd.lanl.gov>
- References: <31000091.3778302@news.panix.com> <19JAN199615253245@erich.triumf.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: bennett@erich.triumf.ca's message of 19 Jan 1996 15:25 PST
-
- --text follows this line--
- In article <19JAN199615253245@erich.triumf.ca> bennett@erich.triumf.ca
- (P.Bennett) writes:
- <snip>
- In article <31000091.3778302@news.panix.com>, dm@panix.com (Dan'l) writes...
- >I am learning C and I have not had any problems understanding most
- >concepts I have learned so far. But to date I still can't figure out
- >how the outcome of this program is 15. Somehow one of the B's ends up
- >a three and the other B a 5, or am I so off base that I can't see
- >what's really happening. Can someone please walk me through this
- >one. Thanks Dan'l
- >
- >#define A 3
- >#define B A + A
-
- This makes B = "3 + 3", not 6 - #defines just do text substitution, not
- arithmetic.
-
- >#define C B * B
-
- Now C = 3 + 3 * 3 + 3. The multiplication will be done first, so you get
- 3 + 9 + 3 = 15
-
- This and a previous mail have both explained it slightly wrong. The
- error is that when you define something, the definition does not get
- expanded immediately: definitions are expanded only when used.
-
- Thus, the correct way to state is that
-
- #define A 3
- #define B A + A
- #define C B * B
-
- stay as they are, but when you write C, the following happens
-
- C gets replaced by
-
- B * B
-
- This is rescanned to get first
-
- A + A * B
-
- The first part is rescanned and so on to get in succession
-
- 3 + A * B
- 3 + 3 * B
- 3 + 3 * A + A
- 3 + 3 * 3 + A
- 3 + 3 * 3 + 3
-
- The nett result is of course the same: in more complicated cases, it
- is better to remember what is going on.
-
- If you _really_ must do something like this, enclose the expressions in
- parenthesis, like so:
- #define A 3
- #define B ((A) + (A))
- #define C ((B) * (B))
-
- I think I have enough parentheses there to ensure things are evaluated in the
- right order... :-)
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-